package net.anzhuo.common.utils;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLEncoder;
import java.security.KeyManagementException;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSession;
import javax.net.ssl.TrustManager;


import javax.net.ssl.X509TrustManager;

import org.jsoup.Connection;
import org.jsoup.Jsoup;
import org.jsoup.helper.HttpConnection;
import org.jsoup.nodes.Document;

import java.security.cert.CertificateException; 
import java.security.cert.X509Certificate;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

public class JSoupUtils {
	
	/**
	 * 信任任何站点，实现https页面的正常访问
	 * 
	 */
	
	public static void trustEveryone() {
        try {  
            HttpsURLConnection.setDefaultHostnameVerifier(new HostnameVerifier() {
                public boolean verify(String hostname, SSLSession session) {
                    return true;  
                }
            });  
  
            SSLContext context = SSLContext.getInstance("TLS");  
            context.init(null, new X509TrustManager[] { new X509TrustManager() {
                public void checkClientTrusted(X509Certificate[] chain, String authType) throws CertificateException {
                }
  
                public void checkServerTrusted(X509Certificate[] chain, String authType) throws CertificateException {
                }
  
                public X509Certificate[] getAcceptedIssuers() {
                    return new X509Certificate[0];  
                }
            } }, new SecureRandom());  
            HttpsURLConnection.setDefaultSSLSocketFactory(context.getSocketFactory());
        } catch (Exception e) {
            // e.printStackTrace();  
        }
    } 

	
	
	
	
		
	public String getKeyNo(String name)
	{
		String result="";
		Map<String,String> headers = new HashMap<>();

        headers.put("User-Agent","Mozilla/5.0 (Windows NT 10.0; WOW64; Trident/7.0; rv:11.0) like Gecko");
        headers.put("Accept", "text/html, application/xhtml+xml, image/jxr, */*");
        headers.put("Accept-Language", "zh-Hans-CN,zh-Hans;q=0.5");
        headers.put("Accept-Encoding", "gzip, deflate");
        headers.put("Host", "www.qichacha.com");
        headers.put("Connection", "Keep-Alive");
        System.setProperty("javax.net.ssl.trustStore", "c://Users//feng//-qichachacom.jks");
		String url="";
		try {
			url = "https://www.qichacha.com/search?key="+URLEncoder.encode(name, "utf-8");
		} catch (UnsupportedEncodingException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		long start = System.currentTimeMillis();
		Document doc=null;
		try{
			doc = Jsoup.connect(url).headers(headers).timeout(5000).get();
		}
		catch(Exception e){
			e.printStackTrace();
		}
		finally{
			System.out.println("Time is:"+(System.currentTimeMillis()-start) + "ms");
		}
		Element elem = doc.getElementById("searchlist");
		if(elem!=null)
		{
		 Elements	TableElem=elem.getElementsByClass("m_srchList");
		 if(TableElem!=null&&TableElem.size()>0)
		 {
			 Element table=TableElem.get(0);
			 Elements trlist=table.getElementsByTag("tr");
			 if(trlist!=null&&trlist.size()>0)
			 {
				Element tr1= trlist.get(0);
				
				Elements tds=tr1.getElementsByTag("td");
				
				if(tds!=null&&tds.size()>0)
				{
					Element td2=tds.get(1);
					
					Elements as=td2.getElementsByTag("a");
					
					if(as!=null&&as.size()>0)
					{
					  Element a=	as.get(0);	  
					  result=a.attr("href");
					}
				}
				
			 }
		 }
		}
		
		return result;
	}
	public static  void main(String[] args) throws IOException{
		JSoupUtils js=new JSoupUtils();
		
		String name="东方安卓";
		String keyNo=js.getKeyNo(name);
		System.out.println(keyNo);
		
		
		
		}	



}
